<?php

class banner {

    /**
     *   @desc Variable Declaration with default value
     */
    private $_id;   // KEY ATTR. WITH AUTOINCREMENT
    private $_title;	private $_description;	private $_link;
    private $_name;
    private $_addeddate;
	private $_updateddate;

    /**
     *   @desc   CONSTRUCTOR METHOD
     */
    function __construct() {
	global $DBOBJ;
	$this->_obj = $DBOBJ;
	$this->_id = null;
	$this->_title = null;		$this->_description = null;	$this->_link = null;
	$this->_name = null;
	$this->_addeddate = date('Y-m-d h:i:s');
	$this->_updateddate = date('Y-m-d h:i:s');
	
    }

    /**
     *   @desc   DECONSTRUCTOR METHOD
     */
    function __destruct() {
	unset($this->_obj);
    }

    /**
     *   @desc   GETTER METHODS
     */
    public function getid() {
	return $this->_id;
    }

    public function gettitle() {
	return $this->_title;
    }
    	public function getdescription() {		return $this->_description;    }		public function getlink() {		return $this->_link;    }

    public function getname() {
	return $this->_name;
    }

    public function getaddeddate() {
	return $this->_addeddate;
    }
	
	
	public function getupdateddate() {
	return $this->_updateddate;
    }
	
	
    
    /**
     *   @desc   SETTER METHODS
     */
    public function setid($id) {
	$this->_id = $id;
    }

    public function settitle($title) {		$this->_title = $title;    }
    	public function setlink($link) {		$this->_link = $link;    }		public function setdescription($description) {		$this->_description = $description;    }	
    public function setname($name) {
	$this->_name = $name;
    }

    public function setaddeddate($addeddate) {
	$this->_addeddate = $addeddate;
    }
	
	public function setupdateddate($updateddate) {
	$this->_updateddate = $updateddate;
    }
	

    /**
     *   @desc   SELECT METHOD / LOAD
     */
    public function select($id) {
	$sql = "SELECT * FROM `banner` WHERE `id` = '" . $id . "' ";
	$row = $this->_obj->select($sql);

	if (count($row) > 0) {
	    $this->_id = $row[0]['id'];
	    $this->_title = $row[0]['title'];				$this->_description = $row[0]['description'];		$this->_link = $row[0]['link'];		
	    $this->_name = $row[0]['name'];
		$this->_addeddate = $row[0]['addeddate'];
		$this->_updateddate = $row[0]['updateddate'];
		
	    return $row[0];
	} else {
	    return;
	}
    }

    /**
     *   @desc   set All
     */
    public function setAll($dataArray = array()) {
	if (count($dataArray) <= 0) {
	    return;
	}

	foreach ($dataArray as $key => $value) {
	    $classKey = '_' . $key;
	    $this->$classKey = $value;
	}
    }

    /**
     *   @desc   get All
     */
    public function getAll($FORMAT = 'ARRAY') {
	$dataArray = new stdClass();
	$dataArray->id = $this->getid();
	$dataArray->title = $this->gettitle();		$dataArray->description = $this->getdescription();	$dataArray->link = $this->getlink();
	$dataArray->name = $this->getname();
	$dataArray->addeddate = $this->getaddeddate();
	$dataArray->updateddate = $this->getupdateddate();
	
	if ($FORMAT == 'ARRAY') {
	    return (array) $dataArray;
	}
	return $dataArray;
    }

    /**
     *   @desc   DELETE
     */
    public function delete($id) {
	$sql = "DELETE FROM `banner` WHERE `id` = '" . $id . "'";
	$result = $this->_obj->sql_query($sql);
    }

    public function deleteImage($name) {
	$sourceFolder = '../upload/images/banner/';
	$imagepath = $sourceFolder . trim($name);
	$thumb_imagepath = $sourceFolder . trim("thumb_".$name);
	if (file_exists($imagepath)) {
	    @unlink($imagepath);
	}
	if (file_exists($thumb_imagepath)) {
	    @unlink($thumb_imagepath);
	}
    }

    /**
     *   @desc   INSERT
     */
    public function insert() {
	$this->id = ""; // clear key for autoincrement
	$sql = "INSERT INTO `banner` ( `id`,`title`,`description`,`link`,`name`,`addeddate` , `updateddate`)
                VALUES ( NULL,
                '" . $this->_title . "',								'" . $this->_description . "',								'" . $this->_link . "',
                '" . $this->_name . "',
				'" . $this->_addeddate . "',
                '" . $this->_updateddate . "' )";
	
	$result = $this->_obj->insert($sql);
	return $result;
    }

    /**
     *   @desc   UPDATE
     */
    public function update($id) {
	$sql = "UPDATE `banner` SET  `id` = '" . $this->_id . "' ,
                `title` = '" . $this->_title . "' ,								`description` = '" . $this->_description . "' ,                `link` = '" . $this->_link . "' ,								`name` = '" . $this->_name . "' ,								
				`updateddate` = '" . $this->_updateddate . "',
                `addeddate` = '" . $this->_addeddate . "'
                WHERE `id` =   '" . $id . "' ";
	$result = $this->_obj->sql_query($sql);
    }

    public function updateImage($id = '', $filename = '') {
	if ($id == '') {
	    return false;
	}
	$updateImageQuery = "UPDATE `banner` SET  `name` = '" . $filename . "' WHERE `id` = '" . $id . "'";
	$this->_obj->sql_query($updateImageQuery);
    }

    public function getAllBannerList($params = '*', $where = '', $orderby = "id", $limit = '') {
	$orderby = 'ORDER BY ' . $orderby;
	$sql = "SELECT " . $params . " FROM `banner` g  " . $where . " " . $orderby . " " . $limit;
	$row = $this->_obj->select($sql);
	if ($row) {
	    return $row;
	}
	return array();
    }

    public function performListAction($action = '', $ids = "''") {
	switch ($action) {
	    case 'delete':
		$idArray = explode(',', $ids);
		if (count($idArray) > 0) {
		    foreach ($idArray as $_id) {
			$this->select($_id);
			$this->deleteImage($this->getname());
		    }
		}
		$sql = "DELETE FROM `banner` WHERE `id` IN (" . $ids . ")";
		break;
	    default:
		$sql = '';
	}
	if ($sql == '') {
	    return false;
	}
	$result = $this->_obj->sql_query($sql);
	return true;
    }

    /**
     *   @desc   COUNT
     */
    public function getCountbanner() {
	$sql = "SELECT count(*) as cnt  FROM `banner`";
	$result = $this->_obj->select($sql);
	return $result['0']['cnt'];
    }

}

?>